package com.itheima.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.ReportDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;

    @Override
    public Map getMemberReport() {
        //获取当前日历时间
        Calendar calendar = Calendar.getInstance();
        //日历向前推一年
        calendar.add(Calendar.MONTH, -12);
        System.out.println(calendar);
        Map<String, List> map = new HashMap<>();
        List<String> months = new ArrayList<>();
        List<Integer> memberCount = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            Date date = calendar.getTime();
            String month = new SimpleDateFormat("yyyy.MM").format(date);
            months.add(month);
        }
        map.put("months", months);
        for (String month : months) {
            String month1 = month + ".31";
            String month2 = month1.replace(".", "-");
            Integer count = reportDao.getMemberCountByMonth(month2);
            memberCount.add(count);
        }

        map.put("memberCount", memberCount);
        return map;
    }

    @Override
    public Map<String, List> getSetmealReport() {
        Map<String, List> map = new HashMap<>();
        List<String> setmealNames = new ArrayList<>();
        List<Map<String, Object>> setmealCount = reportDao.getSetmealAndTotal();
        for (Map<String, Object> list : setmealCount) {
            String value = (String) list.get("name");
            setmealNames.add(value);
        }
        map.put("setmealNames", setmealNames);
        map.put("setmealCount", setmealCount);
        return map;
    }

    @Override
    public Map getBusinessReportData() throws Exception {
        Map<String, Object> map = new HashMap<>();
        String reportData = DateUtils.parseDate2String(new Date());
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        String thisMonthMonday = DateUtils.parseDate2String( DateUtils.getFirstDay4ThisMonth());


        Integer todayNewMember = reportDao.queryTodayNewMember(reportData);
        Integer totalMember = reportDao.queryTotalMember();
        Integer thisWeekNewMember = reportDao.queryNewMember(thisWeekMonday);
        Integer thisMonthNewMember = reportDao.queryNewMember(thisMonthMonday);
        Integer todayOrderNumber = reportDao.queryOrderNumber(reportData);
        Integer todayVisitsNumber = reportDao.queryVisitNumber(reportData);
        Integer thisWeekOrderNumber = reportDao.queryOrderNumber(thisWeekMonday);
        Integer thisWeekVisitsNumber = reportDao.queryVisitNumber(thisWeekMonday);
        Integer thisMonthOrderNumber = reportDao.queryOrderNumber(thisMonthMonday);
        Integer thisMonthVisitsNumber = reportDao.queryVisitNumber(thisMonthMonday);
        List<Map<String, Object>> maps = reportDao.queryHotSetmeal();
        map.put("reportDate", reportData);
        map.put("totalMember", totalMember);
        map.put("todayNewMember", todayNewMember);
        map.put("thisWeekNewMember", thisWeekNewMember);
        map.put("thisMonthNewMember", thisMonthNewMember);
        map.put("todayOrderNumber", todayOrderNumber);
        map.put("todayVisitsNumber", todayVisitsNumber);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        map.put("hotSetmeal", maps);



        return map;
    }


}
